public class LinkedListDeque {
    ublic class LinkedListDeque<T> {
        private class Node {
            T value;
            Node next;

            Node(T value) {
                this.value = value;
                this.next = null;
            }
        }

        private Node first;
        private Node last;
        private int size;

        public LinkedListDeque() {
            first = null;
            last = null;
            size = 0;
        }

        public void addFirst(T num) {
            Node node1 = new Node(num);
            Node temp;
            this.size += 1;
            if (first == null) {
                first = node1;
            }
            temp = this.first;
            this.first = node1;
            node1.next = temp;
        }

        public void addLast(T item) {
            Node node1 = new Node(item);
            last.next = node1;
            node1.next = null;
            this.size += 1;
        }

        public boolean isEmpty() {
            if (first == null) {
                return true;
            } else {
                return false;
            }
        }

        public int size() {
            Node node1 = first;
            int sizeofdeque = 0;
            if (node1 != null) {
                sizeofdeque += 1;
                node1 = node1.next;
            }
            return sizeofdeque;
        }

        public void printDeque() {
            Node node1 = first;
            while (node1 != null) {
                System.out.print(first.value + " ");
                node1 = node1.next;
            }
        }

        public T removeFirst() {
            if (first == null) {
                return null;
            } else if (first == last) {
                this.size = this.size - 1;
                return null;
            } else {
                first = first.next;
                this.size = this.size - 1;
                return first.value;
            }
        }

        public T removeLast() {
            Node node1 = first;
            if (first == null) {
                return null;
            } else if (first == last) {
                this.size -= 1;
                return null;
            } else {
                while (node1.next != null) {
                    node1 = node1.next;
                }
                node1.next = null;
                this.size = this.size - 1;
                return node1.value;
            }
        }

        public T get(int index) {
            Node node1 = first;
            for (int i = 0; i < size; i++) {
                node1 = node1.next;
                if (i == index) {
                    return node1.value;
                }
            }

            return null;
        }
    }



}
